<%--
  Created by IntelliJ IDEA.
  User: 甘叶勇
  Date: 2016/7/21
  Time: 19:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html>
<head>
</head>
<body>
<div id="content-pane">111</div>
<input id="text">
<input type="button" id="send" value="发送">
</body>
<!-- IE8及以下支持JSON -->
<!--[if lt IE 9]>
<script src="https://g.alicdn.com/aliww/ww/json/json.js" charset="utf-8"></script>
<![endif]-->
<!-- WSDK-->
<script src="https://g.alicdn.com/aliww/h5.openim.sdk/1.0.6/scripts/wsdk.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"
        integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script>
    $(function () {
       initEvt();
    });

    function initEvt() {
        $('#send').on('click', function(e) {
            sdk.Tribe.sendMsg({
                tid: '${tid}',
                msg: $('#text').val(),
                success: function(data){
                    buildMsg(data);
                    console.log('发送群消息成功', data);
                },
                error: function(error){
                    console.log('发送群消息失败', error);
                }
            });
        });
    }

    function buildMsg(msg) {
        $('#content-pane').append(msg);
    }
    // 当浏览器不支持时，会alert”对不起,当前浏览器不支持聊天,请更换浏览器
    window.__WSDK__POSTMESSAGE__DEBUG__ = true;

    //捕捉浏览器不支持错误，自定义错误处理
//    window.__WSDK__POSTMESSAGE__DEBUG__ = function(error){
//        console.log(error);
//        alert('浏览器不支持，请切换浏览器');
//    };

    var sdk = new WSDK();

    sdk.Base.login({
        uid: '${uid}',
        appkey: '${appKey}',
        credential: '${credential}',
        timeout: 5000,
        success: function (data) {
            // {code: 1000, resultText: 'SUCCESS'}
            console.log('login success', data);

            sdk.Base.startListenAllMsg();
        },
        error: function (error) {
            // {code: 1002, resultText: 'TIMEOUT'}
            console.log('login fail', error);
        }
    });

    // 匿名登录
//    sdk.Base.login({
//        anonymous: true,
//        appkey: 'appkey',
//        timeout: 5000,
//        success: function(data){
//            // {code: 1000, resultText: 'SUCCESS'}
//            console.log('login success', data);
//        },
//        error: function(error){
//            // {code: 1002, resultText: 'TIMEOUT'}
//            console.log('login fail', error);
//        }
//    });

    var context = {
        name: 'wsdk'
    };

    sdk.Event.on('WSDK_EVENT1 WSDK_EVENT2 WSDK_EVENT3', function(data){
        console.log(data);
        console.log(this.name); // wsdk
    }, context);

    var Event = sdk.Event;

    Event.on('START_RECEIVE_ALL_MSG', function(data){
        buildMsg(data);
        console.log('我所有消息都能收到', data);
    });

    Event.on('MSG_RECEIVED', function(data){
        console.log('我能收到成功的消息', data);
    });

    Event.on('CHAT.MSG_RECEIVED', function(data){
        console.log('我能收到成功的单聊消息', data);
    });

    Event.on('TRIBE.MSG_RECEIVED', function(data){
        console.log('我能收到成功的群聊消息', data);
    });

    Event.on('KICK_OFF', function(data){
        console.log('啊，我被踢了', data);
    });

</script>
</html>
